<?php
/**
 * User: zoudingyong <sheyingtg@qq.com>
 * Date: 2018-6-5 13:08
 */

namespace classes;

use think\Db;
use think\facade\Env;

class WxPay
{


    /**
     * 微信APP支付
     * @param $body
     * @param $out_trade_no
     * @param $money
     * @return array|bool
     */
    public static function WxAppPay($body, $out_trade_no, $money)
    {

        if (!$out_trade_no || $money <= 0) return false;

        $config = [
            'appid' => config('wechat.app_appid'),  // 开放平台账号ID
            'mch_id' => config('wechat.app_mch_id'),// 商户id
            'mch_key' => config('wechat.app_mch_key'),// 秘钥
        ];

        $options = [
            'body' => $body,
            'out_trade_no' => $out_trade_no,
            'total_fee' => $money * 100,
            'trade_type' => 'APP',
            'notify_url' => Env::get('APP_DEBUG') ? config('wechat.notify_url_test') : config('wechat.notify_url'),
            'spbill_create_ip' => request()->ip(),// 客户地址
        ];
        $wechat = new \WeChat\Pay($config);

        $order = $wechat->createOrder($options);
        $result = [
            'appid' => $order['appid'],
            'partnerid' => $order['mch_id'],
            'prepayid' => $order['prepay_id'],
            'package' => "Sign=WXPay",
            'noncestr' => \WeChat\Contracts\Tools::createNoncestr(),
            'timestamp' => (string)time(),
        ];
        $result['sign'] = $wechat->getPaySign($result);
        return $result;
    }


    /**
     * 微信Jsapi/H5支付
     * @param $body
     * @param $openid
     * @param $out_trade_no
     * @param $money
     * @return array|bool
     */
    public static function WxH5Pay($openid, $body, $out_trade_no, $money)
    {

        if (!$out_trade_no || $money <= 0) return false;

        /*$config = [
            'appid' => config('wechat.appid'),  // 开放平台账号ID
            'mch_id' => config('wechat.mch_id'),// 商户id
            'mch_key' => config('wechat.mch_key'),// 秘钥
        ];*/

        $config = [
            'appid' => config('wechat.appid'),  // 公众号APP_ID
            'mch_id' => config('wechat.app_mch_id'),// 绑定的商户id
            'mch_key' => config('wechat.app_mch_key'),// 绑定的商户秘钥
        ];

        $options = [
            'body' => $body,
            'out_trade_no' => $out_trade_no,
            'total_fee' => $money * 100,
            'openid' => $openid,
            'trade_type' => 'JSAPI',
            'notify_url' => Env::get('APP_DEBUG') ? config('wechat.notify_url_test') : config('wechat.notify_url'),
            'spbill_create_ip' => request()->ip(),// 客户地址
        ];
        $wechat = new \WeChat\Pay($config);

        // 生成预支付码
//        $result = $wechat->createOrder($options);
        $order = $wechat->createOrder($options);

        // 创建JSAPI参数签名
        /*$jsApiParameters = $wechat->createParamsForJsApi($result['prepay_id']);

        return $jsApiParameters;*/

        $result = [
            'appId' => config('wechat.appid'),
            'timeStamp' => (string)time(),
            'nonceStr' => \WeChat\Contracts\Tools::createNoncestr(),
            'package' => "prepay_id={$order['prepay_id']}",
            'signType' => "MD5",
        ];
        $result['paySign'] = $wechat->getPaySign($result);
        return $result;
    }

    /**
     * 微信小程序支付
     * @param $body
     * @param $openid
     * @param $out_trade_no
     * @param $money
     * @return array|bool
     */
    public static function WxSmallAppPay($openid, $body, $out_trade_no, $money)
    {

        if (!$out_trade_no || $money <= 0) return false;

        $config = [
            'appid' => config('wechat.wxapp_appid'),
            'mch_id' => config('wechat.wxapp_mch_id'),
            'mch_key' => config('wechat.wxapp_mch_key'),
        ];

        $options = [
            'body' => $body,
            'out_trade_no' => $out_trade_no,
            'total_fee' => $money * 100,
            'openid' => $openid,
            'trade_type' => 'JSAPI',
            'notify_url' => Env::get('APP_DEBUG') ? config('wechat.notify_url_test') : config('wechat.notify_url'),
            'spbill_create_ip' => request()->ip(),
        ];
        $wechat = new \WeChat\Pay($config);
        $order = $wechat->createOrder($options);

        Db::name('jixie_member')
            ->where('open_id', $openid)
            ->update(['prepay_id' => $order['prepay_id']]);

        $result = [
            'appId' => $order['appid'],
            'timeStamp' => (string)time(),
            'nonceStr' => \WeChat\Contracts\Tools::createNoncestr(),
            'package' => "prepay_id={$order['prepay_id']}",
            'signType' => "MD5",
        ];

        $result['paySign'] = $wechat->getPaySign($result);
        return $result;
    }


}